package com.hzz.hellomp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzz.hellomp.entity.User;
import com.hzz.hellomp.mapper.UserMapper;
import com.hzz.hellomp.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.management.QueryEval;
import java.util.List;
//条件构造器
@SpringBootTest
public class T2 {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;
    @Test
    public void testSelectAll(){
        //查询name=Tom的所有用户
        System.out.println("^^^^^^^^^^^^^^^^^^查询name=Tom的所有用户^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("name","tom");
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println("users = " + users);
    }

    @Test
    public void testSelectEmailAll(){
        //查询邮箱域名为baomidou.com的所有用户
        System.out.println("^^^^^^^^^^^^^^^^^^^^查询邮箱域名为baomidou.com的所有用户^^^^^^^^^^^^^^^^^^^^^^^^^");
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("email","baomidou.com");
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println("users = " + users);
    }

    @Test
    public void testSelectOrderDesc(){
        //查询所有用户信息并按照age字段降序排序
        System.out.println("^^^^^^^^^^^^^^^^^^查询所有用户信息并按照age字段降序排序^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("age");
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println("users = " + users);
    }
    @Test
    public void testSelectBetween(){
        //查询age介于[20,30]的所有用户
        System.out.println("^^^^^^^^^^^^^^^^^^^^^查询age介于[20,30]的所有用户^^^^^^^^^^^^^^^^^^^^^^^");
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.between("age",20,30);
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println("users = " + users);
    }
    @Test
    public void testLtAndRt(){
        //查询age小于20或大于30的用户
        System.out.println("^^^^^^^^^^^^^^^^^^^^^查询age介于[20,30]的所有用户^^^^^^^^^^^^^^^^^^^^^^^");
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.lt("age",20).or().lt("age",30);
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println("users = " + users);
    }
    @Test
    public void testMultiCondition(){
        //邮箱域名为baomidou.com且年龄小于30或大于40且的用户
        System.out.println("^^^^^^^^^^^^^^^^^^^^^邮箱域名为baomidou.com且年龄小于30或大于40且的用户^^^^^^^^^^^^^^^^^^^^^^^");
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("email","baomidou.com").and(q -> q.lt("age",20).or().gt("age",30));
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println("users = " + users);
    }

    @Test
    public void testUpdateWrapper(){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name","tom");
        updateWrapper.set("age",60);
        userService.update(updateWrapper);
    }

}
